package com.tanjun.userservice.web.service.role.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tanjun.userservice.web.service.input.AdminUserRoleInput;
import com.tanjun.userservice.web.service.role.BaseAdminUserRoleService;
import com.tanjun.userservice.web.service.role.entity.BaseAdminUserRoleEntity;
import com.tanjun.userservice.web.service.role.mapper.BaseAdminUserRoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author tanjun
* @description 针对表【base_admin_user_role(用户角色表)】的数据库操作Service实现
* @createDate 2024-12-03 21:44:43
*/
@Service
public class BaseAdminUserRoleServiceImpl extends ServiceImpl<BaseAdminUserRoleMapper, BaseAdminUserRoleEntity>
    implements BaseAdminUserRoleService {

    /**
     * 保存管理员用户角色
     *
     * @param paramVo 用户角色信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserRoles(AdminUserRoleInput paramVo) {

        List<BaseAdminUserRoleEntity> listHasRole=this.list(QueryChain.of(BaseAdminUserRoleEntity.class)
                .eq(BaseAdminUserRoleEntity::getUserId,paramVo.getUserId())
        );

        List<Long> hasRoleIdList=listHasRole.stream()
                .map(BaseAdminUserRoleEntity::getRoleId)
                .toList();

        //删除已经移除的权限
        List<Long> listNeedDel=hasRoleIdList.stream()
                .filter(roleId ->!paramVo.getRoleIdList().contains(roleId))
                .collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(listNeedDel)){
            delUserRole(paramVo.getUserId(),listNeedDel);
        }

        //新增增加的权限
        List<Long> listAdd=paramVo.getRoleIdList().stream()
                .filter(t->!hasRoleIdList.contains(t))
                .collect(Collectors.toList());
        if(CollectionUtil.isNotEmpty(listAdd)){
            listAdd.stream()
                    .map(roleId->{
                        BaseAdminUserRoleEntity userRole=new BaseAdminUserRoleEntity();
                        userRole.setUserId(paramVo.getUserId());
                        userRole.setRoleId(roleId);
                        userRole.setCreator(paramVo.getOperator());
                        userRole.setCreateTime(LocalDateTime.now());
                        return userRole;
                    })
                    .forEach(this::save);
        }

    }

    /**
     * 根据用户id 返回用户的权限
     *
     * @param userId 用户id
     * @return 返回用户角色
     */
    @Override
    public List<Long> getUserRoleIds(Long userId){
        List<BaseAdminUserRoleEntity> list= this.list(QueryChain.of(BaseAdminUserRoleEntity.class)
                        .select(BaseAdminUserRoleEntity::getRoleId)
                .eq(BaseAdminUserRoleEntity::getUserId,userId)
        );
        return list.stream().map(BaseAdminUserRoleEntity::getRoleId).collect(Collectors.toList());
    }

    /**
     * 删除用户角色信息
     *
     * @param userId 用户id
     */
    @Override
    public void delUserRole(Long userId) {
        this.remove(QueryChain.of(BaseAdminUserRoleEntity.class)
                .eq(BaseAdminUserRoleEntity::getUserId,userId)
        );
    }

    /**
     * 判断角色是否有用户
     *
     * @param roleId 角色id
     * @return 如果存在return true ,否则return false
     */
    @Override
    public boolean hasUser(Long roleId) {
        return this.count(QueryChain.of(BaseAdminUserRoleEntity.class)
                .eq(BaseAdminUserRoleEntity::getRoleId,roleId)
        )>0;
    }

    /**
     * 删除用户角色
     *
     * @param userId     用户id
     * @param roleIdList 角色ids
     */
    @Override
    public void delUserRole(Long userId, List<Long> roleIdList) {
        this.remove(QueryChain.of(BaseAdminUserRoleEntity.class)
                .eq(BaseAdminUserRoleEntity::getUserId,userId)
                .in(BaseAdminUserRoleEntity::getRoleId,roleIdList)
        );
    }



}




